

import 'dart:convert';

import 'package:flutter_device_info_plugin/flutter_device_info_plugin.dart';
import 'package:furaha_mkopo/api/fm_report_google_market_api.dart';
import 'package:furaha_mkopo/api/fm_report_risk_api.dart';
import 'package:furaha_mkopo/manager/fm_device_manager.dart';

import '../api/fm_report_device_api.dart';
import '../api/fm_report_location_api.dart';

class FMReportEventTool {

  static reportLocation() async {
    dynamic data = await FlutterDeviceInfoPlugin().startLocation();

    if (data is Map) {
      Map<String, dynamic> stringMap = data.map((key, value) => MapEntry(key.toString(), value));

      FMReportLocationApi api = FMReportLocationApi(
          params: stringMap
      );
      api.send().then((value){});
    }
  }

  static reportGoogleMarket() async {

    String idfv = await FMDeviceManager().idfv();
    String idfa = await FMDeviceManager().idfa();

    Map<String,dynamic> params = {
      "economy" : idfv,
      "dint" : idfa
    };

    FMReportGoogleMarketApi api = FMReportGoogleMarketApi(
      params:params
    );
    api.send().then((value){});
  }

  static reportDevice() async {
    String totalDisk = await FMDeviceManager().totalDiskSize();
    String availableDisk = await FMDeviceManager().availableDiskSize();
    String totalMemory = await FMDeviceManager().totalMemorySize();
    String availableMemory = await FMDeviceManager().availableMemorySize();

    String batteryLevel = FMDeviceManager().batteryLevel;
    String batteryState = FMDeviceManager().batteryState ? "1" : "0";

    String sysVersion = await FMDeviceManager().systemVersion();
    String deviceNameplate = await FMDeviceManager().deviceNameplate();
    String originalDeviceInfo = await FMDeviceManager().originalDeviceInfo();

    bool isSimulator = await FMDeviceManager().isSimulator();
    bool isPhoneJailbreak = await FMDeviceManager().isPhoneJailbreak();

    String timeZone = await FMDeviceManager().timeZone();
    String languageCode = await FMDeviceManager().languageCode();
    String netReachableType = await FMDeviceManager().reachableType();
    String idfa = await FMDeviceManager().idfa();
    String idfv = await FMDeviceManager().idfv();

    Map wifiInfo = await FMDeviceManager().wifiInfo();
    String bssid = wifiInfo["BSSID"];
    String ssid = wifiInfo["SSID"];

    Map fold = {
      "old" : availableDisk,
      "fearless" : totalDisk,
      "also" : totalMemory,
      "commerce" : availableMemory
    };

    Map anyone = {
      "clasped" : batteryLevel,
      "thanks" : batteryState
    };

    Map katrine = {
      "dundee" : sysVersion,
      "lived" : deviceNameplate,
      "motionless" : originalDeviceInfo,
    };

    Map arrival = {
      "sympathy" : isSimulator ? "1" : "0",
      "able" : isPhoneJailbreak ? "1" : "0"
    };

    Map voyages = {
      "answering" : timeZone,
      "economy" : idfv,
      "actual" : languageCode,
      "interrogate" : netReachableType,
      "dint" : idfa
    };

    Map remain = {
      "returns" : {
        "increased" : bssid,
        "planned" : ssid
      }
    };

    Map json = {
      "fold" : fold,
      "anyone" : anyone,
      "katrine" : katrine,
      "arrival" : arrival,
      "voyages" : voyages,
      "remain" : remain
    };

    FMReportDeviceApi api = FMReportDeviceApi(
      jsonString: jsonEncode(json)
    );

    api.send().then((value){});
  }

  static reportRisk(String type,double? latitude,double? longitude,int startTime,int endTime,{String? orderId}) async {

    String idfv = await FMDeviceManager().idfv();
    String idfa = await FMDeviceManager().idfa();

    Map<String,dynamic> params = {
      "regularly" : type,
      "explore" : "2",
      "leaving" : idfv,
      "picked" : idfa,
      "prudence" : longitude == null || longitude == 0 ? "0" : longitude.toStringAsFixed(6),
      "support" : latitude == null || latitude == 0 ? "0" : latitude.toStringAsFixed(6),
      "manned" : startTime.toString(),
      "cause" : endTime.toString(),
      "placed" : orderId ?? ""
    };

    FMReportRiskApi api = FMReportRiskApi(
      params: params
    );

    api.send().then((value){
      print(value);
    });
  }
}